from flask import request, current_app, jsonify

from apps.apis import api
from apps.core.auth import verify_json_schema
from apps.core.database import db
from apps.core.schemaall import admin_damin_schema
from apps.models import User
from apps.utils.return_info import ReturnInfo

# 废弃该接口
@api.route("/adminadmin/admin", methods=['POST'])
@verify_json_schema(admin_damin_schema)
def admin_damin():
    """
    本接口是为了防止admin用户忘记密码而设计,可以强制修改或添加admin的账号和密码
    需要特殊的验证机制
    ---
    tags:
      - adminadmin
    parameters:
      - in: body
        name: body
        required: true
        description: JSON 参数.
        schema:
          id: addusers
          properties:
            passwd:
              type: string
              description: admin的密码.
              example: Smith123*
            ipaddr:
              type: string
              description: 限制用户的ip地址.
              example: 192.168.31.123
            admin_admin:
              type: string
              description: admin_damin 账户 与配置文件相同才行.
              example: admin_admin
    consumes:
      - application/json
    produces:
      - application/json
    definitions:
      ReturnInfoAdminAdmin:
        type: object
        properties:
          msg_code:
            type: string
            description: SUCCESS 或者 FAILED.
            example: SUCCESS
          msg:
            type: string
            description: 说明文字，帮助找到失败原因
            example: 不能使用get请求
          data:
            type: object
            description: 返回的信息全部在data中获取
            properties:
              result:
                type: object
                properties:
                  username:
                    type: string
                    description: 添加的用户名
    responses:
      200:
        description: OK.
        schema:
          $ref: '#/definitions/ReturnInfoAdminAdmin'
        examples:
          returninfo: {"msg_code":"SUCCESS","msg":"成功","data":{"result": {"username":"Alice"}}}
      400:
        description: 上传数据有不符合规则,data中放入的是上传的数据.
        examples:
          returninfo: {"msg_code":"FAILED","msg":"参数至少一个为空","data":{"ipaddr": "192.168.31.127","passwd": "","username": "Alice"}}
      405:
        description: 请求方式错误

    """
    ret_obj = ReturnInfo()
    if current_app.config.get("ADMIN_ADMIN") == "":
        ret_obj.msg_code = "FAILED"
        ret_obj.msg = ".env 没有配置ADMIN_ADMIN,不允许对admin账户更改"
        ret_obj.data = ""
        return jsonify(ret_obj.todict())
    jsons = request.json
    passwd = jsons.get("passwd")
    ipaddr = jsons.get("ipaddr")
    admin_damin = jsons.get("admin_damin")
    if not admin_damin == current_app.config.get("ADMIN_ADMIN"):
        ret_obj.msg_code = "FAILED"
        ret_obj.msg = "admin_admin 账户与配置文件的账户不匹配，不允许更改"
        ret_obj.data = ""
        return jsonify(ret_obj.todict())

    user = User.query.filter_by(username=current_app.config.get("ADMIN_USER")).first()
    if user:
        if not user.passwd_rule(passwd):
            ret_obj.msg_code = "FAILED"
            ret_obj.msg = "密码不符合规则，必须包含数字、字母和一个特殊符号! @ # $ % ^ * ( ) ~ `{ } [ ] | & _"
            return jsonify(ret_obj.todict())
        user.hash_password(passwd)
        user.ip = ipaddr
        db.session.add(user)
        db.session.commit()
        ret_obj.msg = "修改密码成功"
    else:
        user = User(username=current_app.config.get("ADMIN_USER"), ip=ipaddr)
        if not user.passwd_rule(passwd):
            ret_obj.msg_code = "FAILED"
            ret_obj.msg = "密码不符合规则，必须包含数字、字母和一个特殊符号! @ # $ % ^ * ( ) ~ `{ } [ ] | & _"
            return jsonify(ret_obj.todict())
        user.hash_password(passwd)
        db.session.add(user)
        db.session.commit()
        ret_obj.msg = "添加成功"
    ret_obj.msg_code = "SUCCESS"
    ret_obj.data = {"result": {"username": current_app.config.get("ADMIN_USER")}}
    return jsonify(ret_obj.todict())
